package com.sike.netty.jinjie.bytebuf.api;

import io.netty.buffer.*;
import org.apache.logging.log4j.spi.CopyOnWrite;

import java.nio.ByteBuffer;
import java.nio.charset.Charset;

public class CompositeByteBufTest {
    public static void main(String[] args) {
        CompositeByteBuf compositeByteBuf = ByteBufAllocator.DEFAULT.compositeBuffer();
        ByteBufPrintUtil.printByteBufDetail(compositeByteBuf,"new compositeByteBuf");

        ByteBuf buffer1 = ByteBufAllocator.DEFAULT.buffer(4,8);
        buffer1.writeBytes(new byte[]{'a','b'});
        ByteBufPrintUtil.printByteBufDetail(buffer1,"buffer1");

        // 添加 buffer1
        compositeByteBuf.addComponent(true,buffer1);

        ByteBufPrintUtil.printByteBufDetail(compositeByteBuf,"addComponent(buffer1)");

        ByteBuf buffer2 = ByteBufAllocator.DEFAULT.buffer(5,10);
        buffer2.writeBytes(new byte[]{'h','i','j'});
        ByteBufPrintUtil.printByteBufDetail(buffer2,"buffer2");

        compositeByteBuf.addComponent(true,buffer2);
        ByteBufPrintUtil.printByteBufDetail(compositeByteBuf,"addComponent(buffer2)");

        buffer1.readByte();
        buffer2.readByte();
        ByteBufPrintUtil.printByteBufDetail(buffer1,"buffer1");
        ByteBufPrintUtil.printByteBufDetail(buffer2,"buffer2");
        ByteBufPrintUtil.printByteBufDetail(compositeByteBuf,"compositeByteBuf");

        buffer1.setByte(1,'x');
        buffer2.setByte(1,'y');
        ByteBufPrintUtil.printByteBufDetail(compositeByteBuf,"compositeByteBuf");

        buffer1.writeBytes(new byte[]{'o'});
        buffer2.writeBytes(new byte[]{'z'});
        ByteBufPrintUtil.printByteBufDetail(buffer1,"buffer1");
        ByteBufPrintUtil.printByteBufDetail(buffer2,"buffer2");
        // 调整 compositeByteBuf 的指针，要不 compositeByteBuf 读不到
        compositeByteBuf.capacity(10);
        compositeByteBuf.writerIndex(10);
        ByteBufPrintUtil.printByteBufDetail(compositeByteBuf,"compositeByteBuf");
    }
}
